﻿Imports TNCDAL

Public Class frmFile_PhieuTamUng
    Dim db As New TanNamChinhDataContext
    Private m_ID_AdvanceVoucher As Integer = 0
    Private m_ID_HBL As Integer = 0

    Property ID_AdvanceVoucher As Integer
        Get
            Return m_ID_AdvanceVoucher
        End Get
        Set(ByVal value As Integer)
            m_ID_AdvanceVoucher = value
        End Set
    End Property

    Property ID_HBL As Integer
        Get
            Return m_ID_HBL
        End Get
        Set(ByVal value As Integer)
            m_ID_HBL = value
        End Set
    End Property

    Private Sub frmFile_PhieuTamUng_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KhoiTaodtgPhieuTamUng()
        KhoiTaodtgHBL(Nothing)
        rbtAll.Checked = True
        rbtImportExport.Checked = True
    End Sub

    Public Sub KhoiTaodtgHBL(ByVal IsExport As Object) ' 0Tat ca, 1 Ex, 2 Im
        'Khoi tao danh sach file va HBL ma chua lap phieu thu
        Dim HBL_SellingFee = (From p In db.tblSellingFees
                      Select p.ID_ReceiptVoucher).Distinct.ToList

        If IsExport Is Nothing Then
            Dim listHBL = From p In db.tblHBLs
                      Where Not (
                                    (From q In db.tblSellingFees
                                    Where Not q.ID_ReceiptVoucher Is Nothing
                                    Select q.ID_HBL).Distinct()
                                ).Contains(p.ID_HBL)
                      Order By p.tblFileMBL.tblFile.IsExport, p.tblFileMBL.tblFile.FileNo
                      Select FileNo = p.tblFileMBL.tblFile.FileNo.ToString + IIf(p.tblFileMBL.tblFile.IsExport, "/EX", "/IM"),
                                    p.tblFileMBL.tblFile.CreatedDate,
                                    p.HBLNo,
                                    p.tblFileMBL.tblFile.tblCustomer.CustomerName,
                                    p.tblFileMBL.tblFile.tblCompany.Name,
                                    p.tblFileMBL.tblMBL.tblVoyage.tblVessel.VesselName,
                                    p.ID_HBL
            'Order By FileNo

            Dim source As New BindingSource
            source.DataSource = listHBL
            BindingNavigator1.BindingSource = source
            dtgHBL.DataSource = source
            FormatdtgHBL()
        Else
            Dim listHBL = From p In db.tblHBLs
                     Where Not (
                                   (From q In db.tblSellingFees
                                   Where Not q.ID_ReceiptVoucher Is Nothing
                                   Select q.ID_HBL).Distinct()
                               ).Contains(p.ID_HBL) And p.tblFileMBL.tblFile.IsExport = CBool(IsExport)
                     Order By p.tblFileMBL.tblFile.IsExport, p.tblFileMBL.tblFile.FileNo
                     Select FileNo = p.tblFileMBL.tblFile.FileNo.ToString + IIf(p.tblFileMBL.tblFile.IsExport, "/EX", "/IM"),
                                   p.tblFileMBL.tblFile.CreatedDate,
                                   p.HBLNo,
                                   p.tblFileMBL.tblFile.tblCustomer.CustomerName,
                                   p.tblFileMBL.tblFile.tblCompany.Name,
                                   p.tblFileMBL.tblMBL.tblVoyage.tblVessel.VesselName,
                                   p.ID_HBL
            'Order By FileNo

            Dim source As New BindingSource
            source.DataSource = listHBL
            BindingNavigator1.BindingSource = source
            dtgHBL.DataSource = source
            FormatdtgHBL()
        End If
    End Sub

    Public Sub FormatdtgHBL()
        dtgHBL.RowHeadersWidth = 20
        dtgHBL.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgHBL.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dtgHBL.Columns("FileNo").Frozen = True

        dtgHBL.MultiSelect = False
        dtgHBL.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgHBL.ReadOnly = True
        dtgHBL.BackgroundColor = Color.White
        dtgHBL.AllowUserToAddRows = False
        dtgHBL.AllowUserToDeleteRows = False

        dtgHBL.Columns("ID_HBL").Visible = False
        With dtgHBL.Columns("FileNo")
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Width = 40
        End With
        With dtgHBL.Columns("CustomerName")
            .HeaderText = "Customer"
            .Width = 80
        End With
        With dtgHBL.Columns("Name")
            .HeaderText = "Company"
            .Width = 150
        End With
        With dtgHBL.Columns("CreatedDate")
            .Width = 80
            .DefaultCellStyle.Format = "dd/MM/yyyy"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
    End Sub

    Public Sub KhoiTaodtgPhieuTamUng()
        'Khoi tao danh sach phieu tam ung ma chua lap phieu thu
        Dim listAV = From p In db.tblAdvanceVouchers
                     Where p.ID_ReceiptVoucher Is Nothing
                     Select p.ID_AdvanceVoucher, p.AdvanceVoucherNo, p.CreatedDate, p.Description, p.Amount, p.tblUser1.FullName
                     Order By AdvanceVoucherNo

        Dim source As New BindingSource
        source.DataSource = listAV
        BindingNavigator2.BindingSource = source
        dtgAdvanceVoucher.DataSource = source
        FormatdtgPhieuTamUng()
    End Sub

    Public Sub FormatdtgPhieuTamUng()
        dtgAdvanceVoucher.RowHeadersWidth = 20
        dtgAdvanceVoucher.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgAdvanceVoucher.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dtgAdvanceVoucher.Columns("AdvanceVoucherNo").Frozen = True

        dtgAdvanceVoucher.MultiSelect = False
        dtgAdvanceVoucher.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgAdvanceVoucher.ReadOnly = True
        dtgAdvanceVoucher.BackgroundColor = Color.White
        dtgAdvanceVoucher.AllowUserToAddRows = False
        dtgAdvanceVoucher.AllowUserToDeleteRows = False

        dtgAdvanceVoucher.Columns("ID_AdvanceVoucher").Visible = False
        dtgAdvanceVoucher.Columns("Description").Width = 120
        With dtgAdvanceVoucher.Columns("FullName")
            .HeaderText = "Người tạm ứng"
            .Width = 80
        End With
        With dtgAdvanceVoucher.Columns("AdvanceVoucherNo")
            .HeaderText = "AV No"
            .Width = 40
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
        With dtgAdvanceVoucher.Columns("CreatedDate")
            .Width = 80
            .DefaultCellStyle.Format = "dd/MM/yyyy"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
        With dtgAdvanceVoucher.Columns("Amount")
            .Width = 100
            .DefaultCellStyle.Format = "#,###"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With
    End Sub

    Private Sub dtgAdvanceVoucher_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgAdvanceVoucher.CellDoubleClick
        m_ID_AdvanceVoucher = dtgAdvanceVoucher.Rows(e.RowIndex).Cells("ID_AdvanceVoucher").Value
        Me.Close()
    End Sub

    Private Sub dtgHBL_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgHBL.CellDoubleClick
        m_ID_HBL = dtgHBL.Rows(e.RowIndex).Cells("ID_HBL").Value
        Me.Close()
    End Sub


#Region "Che do xem"
    Private Sub rbtAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtAll.CheckedChanged
        Try
            If rbtAll.Checked Then
                SplitContainer1.Panel2Collapsed = False
                SplitContainer1.Panel1Collapsed = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub rbtHBL_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtHBL.CheckedChanged
        If rbtHBL.Checked Then
            SplitContainer1.Panel2Collapsed = True
        End If
    End Sub

    Private Sub rbtAdvanceVoucher_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtAdvanceVoucher.CheckedChanged
        If rbtAdvanceVoucher.Checked Then
            SplitContainer1.Panel1Collapsed = True
        End If
    End Sub
#End Region

#Region "Loc HBL"
    Private Sub rbtImportExport_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtImportExport.CheckedChanged
        If rbtImportExport.Checked Then
            KhoiTaodtgHBL(Nothing)
        End If
    End Sub

    Private Sub rbtFileExport_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtFileExport.CheckedChanged
        If rbtFileExport.Checked Then
            KhoiTaodtgHBL(True)
        End If
    End Sub

    Private Sub rbtFileImport_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtFileImport.CheckedChanged
        If rbtFileImport.Checked Then
            KhoiTaodtgHBL(False)
        End If
    End Sub
#End Region
End Class